package com.hnld.transform.handler.impl;

import cn.hutool.core.io.FileUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hnld.transform.dictionary.Directory;
import com.hnld.transform.entity.Parameter;
import com.hnld.transform.enums.FileTypeEnum;
import com.hnld.transform.handler.IOtherFileHandler;
import com.hnld.transform.handler.ITypeHandler;
import com.hnld.transform.util.FindChildrenClassUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class OtherHandler implements ITypeHandler {
    private static List<String> targets = Lists.newArrayList();
    private static Map<String, String> sourceMap = Maps.newHashMap();
    private static String routePath = "/路面图像/";

    static Map<String, IOtherFileHandler> fileHandlerMap = new HashMap<>();

    static {
        //
        targets.add("DR");
        targets.add("IRI");
        targets.add("LBI");
        targets.add("RIFile");
        //对应的源文件夹目录
        sourceMap.put("DR", "/路面图像/识别结果/");
        sourceMap.put("IRI", "/平整度数据/IRI/");
        sourceMap.put("LBI", "/LBI/");
        sourceMap.put("RIFile", "/平整度数据/RIFile/");

        try {
            fileHandlerMap = FindChildrenClassUtil.findImplementations(IOtherFileHandler.class, "getType");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getType() {
        return "非图像类数据";
    }

    @Override
    public void handleTypeData(String path, String area, Parameter parameter) {
        System.out.println("处理区县" + area + "的非图像类数据");
        String baseSourcePath = parameter.getBaseSourcePath() + area + "\\";
        //获取路线，区县内的上下行库中获取
        String sourceRoutePath = parameter.getBaseSourcePath() + "上下行库.txt";
        List<String> allRouteList = FileUtil.readLines(sourceRoutePath, "UTF-8");
        List<String> areaRouteList = allRouteList.stream().filter(r -> r.contains(area)).distinct()
                .map(r -> r.replaceAll(area, "").trim())
                .collect(Collectors.toList());
        //上下行库中是否存在两个方向的路线编码
        Map<String, Integer> twoDirectionsMap = new HashMap(16);
        areaRouteList.stream().forEach(routeCode -> {
            String substringed = routeCode.substring(0, routeCode.length() - 1);
            if (twoDirectionsMap.isEmpty()) {
                twoDirectionsMap.put(substringed, 1);
            } else {
                Integer integer = Optional.ofNullable(twoDirectionsMap.get(substringed)).orElse(0);
                twoDirectionsMap.put(substringed, integer == 0 ? integer : integer + 1);
            }
        });
        //["C001430182A","C004433127B"]
        for (String areaRoute : areaRouteList) {
            //创建路线目录  /.../非图像类数据/+路线编码
            String routeDir = Directory.createDirectory(path, areaRoute, FileTypeEnum.DIRECTORY);
            //创建文件类型目录 DR,IRI,LBI,RIFile /.../非图像类数据/路线编码+/DR,IRI,LBI,RIFile
            for (String target : targets) {
                //原路径地址
                String sourceDir = sourceMap.get(target);
                //创建目标路径 /.../非图像类数据/路线编码+/DR,IRI,LBI,RIFile
                String targetDir = Directory.createDirectory(routeDir, target, FileTypeEnum.DIRECTORY);
                //读取对应原文件夹中的文件
                String sourcePath = baseSourcePath + sourceDir;
                List<File> fileList = Lists.newArrayList();
                //RIFile单独处理
                if (!"RIFile".equals(target)) {
                    fileList = FileUtil.loopFiles(sourcePath);
                }
                fileHandlerMap.get(target).handleFileType(fileList, targetDir, area, areaRoute, parameter, twoDirectionsMap);
            }
//            String zipPath = routeDir+".zip";
//            ZipUtil.zip(routeDir,zipPath);
            //FileUtil.del(routeDir);
        }

    }

    public static void main(String[] args) {
        List<File> fileList = FileUtil.loopFiles("E:\\报部数据转格式\\零陵区\\路面图像\\识别结果");
        System.out.println(fileList.size());
    }

}
